package com.ch.base.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TextServlet extends HttpServlet { 
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		String fileUri = request.getRequestURI(); 
		String contextPath = request.getContextPath();
		fileUri = fileUri.substring(contextPath.length());
		BufferedReader br = null;
		try{
			InputStream is = getClass().getResourceAsStream(fileUri);
			br  = new BufferedReader(new InputStreamReader(is, "utf-8")); 
			PrintWriter out  =  response.getWriter(); 
			response.setContentType("charset=utf-8");
			response.setCharacterEncoding("utf-8");
			String line = null; 
			while ((line = br.readLine()) != null){ 
				out.println(line); 
			} 
			br.close(); 
			out.close(); 
		} catch (Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (br != null){
				try{
					br.close();
				} catch (IOException e){
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	} 
} 